#region Copyright & License Information
/*
 * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
 * This file is part of OpenRA, which is free software. It is made
 * available to you under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version. For more
 * information, see COPYING.
 */
#endregion

using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;

namespace OpenRA.Mods.Common.Graphics
{
	public class ContrailRenderable : IRenderable, IFinalizedRenderable
	{
		const int MaxSmoothLength = 4;

		public int Length { get { return trail.Length; } }

		readonly World world;
		readonly Color startcolor;
		readonly Color endcolor;

		readonly bool useOutColor;
		readonly Color outstartcolor;
		readonly Color outendcolor;

		readonly int zOffset;
		readonly float fadeWithWidthRate;
		readonly BlendMode blendmode = BlendMode.Alpha;

		// Store trail positions in a circular buffer
		readonly WPos[] trail;
		readonly WDist width;
		int next;
		int length;
		int skip;

		public ContrailRenderable(World world, Color startcolor, Color endcolor, WDist width, int length, int skip, int zOffset, float fadeWithWidthRate, BlendMode blendmode = BlendMode.Alpha)
			: this(world, new WPos[length], width, 0, 0, skip, startcolor, endcolor, zOffset, fadeWithWidthRate, blendmode) { }

		public ContrailRenderable(World world, Color startcolor, Color endcolor, WDist width, int length, int skip, int zOffset, float fadeWithWidthRate, BlendMode blendmode, bool useOutColor, Color outStart, Color outEnd)
			: this(world, new WPos[length], width, 0, 0, skip, startcolor, endcolor, zOffset, fadeWithWidthRate, blendmode, useOutColor, outStart, outEnd) { }

		public ContrailRenderable(World world, WPos[] trail, WDist width, int next, int length, int skip, Color startcolor, Color endcolor, int zOffset, float fadeWithWidthRate, BlendMode blendmode = BlendMode.Alpha)
		{
			this.world = world;
			this.trail = trail;
			this.width = width;
			this.next = next;
			this.length = length;
			this.skip = skip;
			this.startcolor = startcolor;
			this.endcolor = endcolor;
			useOutColor = false;
			outstartcolor = startcolor;
			outendcolor = endcolor;
			this.zOffset = zOffset;
			this.fadeWithWidthRate = fadeWithWidthRate;
			this.blendmode = blendmode;
		}

		public ContrailRenderable(World world, WPos[] trail, WDist width, int next, int length, int skip, Color startcolor, Color endcolor, int zOffset, float fadeWithWidthRate, BlendMode blendmode, bool useOutColor, Color outstartcolor, Color outendcolor)
		{
			this.world = world;
			this.trail = trail;
			this.width = width;
			this.next = next;
			this.length = length;
			this.skip = skip;
			this.startcolor = startcolor;
			this.endcolor = endcolor;
			this.useOutColor = useOutColor;
			this.outstartcolor = outstartcolor;
			this.outendcolor = outendcolor;
			this.zOffset = zOffset;
			this.fadeWithWidthRate = fadeWithWidthRate;
			this.blendmode = blendmode;
		}

		public WPos Pos { get { return trail[Index(next - 1)]; } }
		public int ZOffset { get { return zOffset; } }
		public bool IsDecoration { get { return true; } }

		public IRenderable WithZOffset(int newOffset) { return new ContrailRenderable(world, (WPos[])trail.Clone(), width, next, length, skip, startcolor, endcolor, newOffset, fadeWithWidthRate, blendmode, useOutColor, outstartcolor, outendcolor); }
		public IRenderable OffsetBy(WVec vec)
		{
			// Lambdas can't use 'in' variables, so capture a copy for later
			var offset = vec;
			return new ContrailRenderable(world, trail.Select(pos => pos + offset).ToArray(), width, next, length, skip, startcolor, endcolor, zOffset, fadeWithWidthRate, blendmode, useOutColor, outstartcolor, outendcolor);
		}

		public IRenderable AsDecoration() { return this; }

		public IFinalizedRenderable PrepareRender(WorldRenderer wr) { return this; }
		public void Render(WorldRenderer wr)
		{
			// Note: The length of contrail is now actually the number of the points to draw the contrail
			// and we require at least two points to draw a tail
			var renderLength = length - skip;
			if (renderLength <= 1)
				return;

			var screenWidth = wr.ScreenVector(new WVec(width, WDist.Zero, WDist.Zero))[0];
			var wcr = Game.Renderer.WorldRgbaColorRenderer;

			// Start of the first line segment is the tail of the list - don't smooth it.
			var curPos = trail[Index(next - skip - 1)];
			var curColor = startcolor;

			for (var i = 1; i < renderLength; i++)
			{
				var j = next - skip - 1 - i;
				var nextColor = Exts.ColorLerp(i * 1f / (renderLength - 1), startcolor, endcolor);

				var nextX = 0L;
				var nextY = 0L;
				var nextZ = 0L;
				var k = 0;
				for (; k < renderLength - i && k < MaxSmoothLength; k++)
				{
					var prepos = trail[Index(j - k)];
					nextX += prepos.X;
					nextY += prepos.Y;
					nextZ += prepos.Z;
				}

				var nextPos = new WPos((int)(nextX / k), (int)(nextY / k), (int)(nextZ / k));

				if (!world.FogObscures(curPos) && !world.FogObscures(nextPos))
				{
					var wfade = (renderLength * 1f - (i - 1) * fadeWithWidthRate) / renderLength;
					if (wfade > 0)
						if (useOutColor)
							wcr.DrawLine(wr.Screen3DPosition(curPos), wr.Screen3DPosition(nextPos), screenWidth * wfade, curColor, nextColor, blendmode, outstartcolor, outendcolor);
						else
							wcr.DrawLine(wr.Screen3DPosition(curPos), wr.Screen3DPosition(nextPos), screenWidth * wfade, curColor, nextColor, blendmode);
				}

				curPos = nextPos;
				curColor = nextColor;
			}
		}

		public void RenderDebugGeometry(WorldRenderer wr) { }
		public Rectangle ScreenBounds(WorldRenderer wr) { return Rectangle.Empty; }

		// Array index modulo length
		int Index(int i)
		{
			var j = i % trail.Length;
			return j < 0 ? j + trail.Length : j;
		}

		public void Update(WPos pos)
		{
			trail[next] = pos;
			next = Index(next + 1);

			if (length < trail.Length)
				length++;
		}

		public static ContrailRenderable CopyContrailRenderable(ContrailRenderable contrail)
		{
			return new ContrailRenderable(contrail.world, (WPos[])contrail.trail.Clone(), contrail.width, contrail.next, contrail.length, contrail.skip, contrail.startcolor, contrail.endcolor, contrail.zOffset, contrail.fadeWithWidthRate, contrail.blendmode, contrail.useOutColor, contrail.outstartcolor, contrail.outendcolor);
		}
	}
}
